package com.carple.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.carple.dao.LoveBoardDao;
import com.carple.dao.MemberDao;
import com.carple.dto.LoveBoardComment;
import com.carple.dto.Member;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/loveboard/writecomment.action")
public class LoveBoardCommentWriteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1. dto객체 생성 -> 입력 정보 읽어서 dto객체에 저장
		int boardNo = Integer.parseInt(request.getParameter("boardno"));

		LoveBoardComment comment = new LoveBoardComment();
		comment.setContent(request.getParameter("content"));
		comment.setBoardNo(boardNo);
		comment.setEmailId(((Member) request.getSession().getAttribute("loginuser")).getEmailId());

		// 2. 데이터베이스에 데이터 insert
		LoveBoardDao dao = new LoveBoardDao();
		dao.insertComment(comment);

		// 3. boardview로 이동
		String pageNo = request.getParameter("pageno");
		response.sendRedirect("view.action?boardno=" + boardNo + "&pageno="
				+ pageNo);

	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		doGet(request, response);
	}
}
